package pl.droidsonroids.gif;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;

/**
 * An {@link ImageView} which tries treating background and src as {@link GifDrawable}
 *
 * @author koral--
 */
public class GifImageView extends Image implements Component.DrawTask {
    private static final String TAG_LOG = "GifImageView";

    private static final int DOMAIN_ID = 0xD000F00;

    private static final String LOG_FORMAT = "%{public}s: %{public}s";

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);

    private GifDrawable mGifDrawable;
    private GifDrawable mBackground;
    private boolean mFreezesAnimation;
    private AttrSet mAttrs;

    /**
     * A corresponding superclass constructor wrapper.
     *
     * @param context context
     * @see Image#Image(Context)
     */
    public GifImageView(Context context) {
        super(context);
        addDrawTask(this);
    }

    /**
     * Like equivalent from superclass but also try to interpret src and background
     * attributes as {@link GifDrawable}.
     *
     * @param context context
     * @param attrs attrs
     * @throws NotExistException NotExistException
     * @throws WrongTypeException WrongTypeException
     * @throws IOException IOException
     * * @see Image#Image (Context, AttrSet)
     */
    public GifImageView(Context context, AttrSet attrs) throws NotExistException, WrongTypeException, IOException {
        super(context, attrs);
        this.mAttrs = attrs;
        addDrawTask(this);
    }

    /**
     * Like equivalent from superclass but also try to interpret src and background
     * attributes as GIFs.
     *
     * @param context context
     * @param attrs attrs
     * @param defStyle defStyle
     * @throws NotExistException NotExistException
     * @throws WrongTypeException WrongTypeException
     * @throws IOException IOException
     * @see Image#Image (Context, AttrSet, int)
     */
    public GifImageView(Context context, AttrSet attrs, String defStyle)
            throws NotExistException, WrongTypeException, IOException {
        super(context, attrs, defStyle);
        this.mAttrs = attrs;
        addDrawTask(this);
    }

    /**
     * Like equivalent from superclass but also try to interpret src and background
     * attributes as GIFs.
     *
     * @param context context
     * @param attrs attrs
     * @param defStyle defStyle
     * @param defStyleRes defStyleRes
     * @throws NotExistException NotExistException
     * @throws WrongTypeException WrongTypeException
     * @throws IOException IOException
     *
     */

    public GifImageView(Context context, AttrSet attrs, String defStyle, int defStyleRes)
            throws NotExistException, WrongTypeException, IOException {
        super(context, attrs, defStyle);
        this.mAttrs = attrs;
        addDrawTask(this);
    }

    private void postInit(GifViewUtils.GifImageViewAttributes result)
            throws NotExistException, WrongTypeException, IOException {
        mFreezesAnimation = result.freezesAnimation;
        if (result.mSourceResId > 0) {
            super.setImageElement(GifViewUtils.getElement(getContext(), result.mSourceResId));
        }
        if (result.mBackgroundResId > 0) {
            super.setBackground(GifViewUtils.getElement(getContext(), result.mBackgroundResId));
        }
    }

    @Override
    public void setPixelMap(int resId) {
        try {
            if (!GifViewUtils.setResource(this, true, resId)) {
                super.setPixelMap(resId);
            }
        } catch (IOException e) {
            HiLog.error(LABEL_LOG, LOG_FORMAT, TAG_LOG, "IoException");
        }
    }

    /**
     * setImageElement
     *
     * @param resId resId
     * @throws IOException IOException
     * @throws NotExistException NotExistException
     */
    public void setImageElement(int resId) throws IOException, NotExistException {
        if (!GifViewUtils.setResource(this, true, resId)) {
            super.setImageElement(GifViewUtils.getElement(getContext(), resId));
        }
    }

    /**
     * setImageElement
     *
     * @param element element
     */
    public void setImageElement(GifDrawable element) {
        mGifDrawable = element;
        super.setImageElement(mGifDrawable);
        mGifDrawable.setElementCallback(this);
    }

    /**
     * setBackgroundElement
     *
     * @param backgroundElement backgroundElement
     */
    public void setBackgroundElement(GifDrawable backgroundElement) {
        mBackground = backgroundElement;
        super.setBackground(mBackground);
        mBackground.setElementCallback(this);
    }

    /**
     * setBackgroundResource
     *
     * @param resId resId
     * @throws NotExistException NotExistException
     * @throws IOException IOException
     */
    public void setBackgroundResource(int resId) throws NotExistException, IOException {
        if (!GifViewUtils.setResource(this, false, resId)) {
            super.setBackground(GifViewUtils.getElement(getContext(), resId));
        }
    }

    /**
     * To set the Freeze animation
     * @param freezesAnimation freezesAnimation
     */
    public void setFreezesAnimation(boolean freezesAnimation) {
        mFreezesAnimation = freezesAnimation;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mGifDrawable != null) {
            mGifDrawable.drawToCanvas(canvas);
        }
        if (mBackground != null) {
            mBackground.drawToCanvas(canvas);
        }
    }
}
